﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Code2html
{
    public class Program
    {
        public static int Main(string[] args)
        {
            System.Reflection.Assembly a = System.Reflection.Assembly.GetEntryAssembly();
            string baseDir = Path.GetDirectoryName(a.Location);
            string path = "";
            string p = "sdfg\"fghj";
            string d = @"asdf""sdf";
            char g = '\'';
            StreamReader ReadCode;
            //StreamWriter WriteCode;
            Console.OutputEncoding = Encoding.GetEncoding(866);
            string input = "", output = "", include = "", exclude = "", lang = "", color = "";
            bool recursive = false, mshelp = false, index = false, err = false;
            if (args.Length == 0)
            {
                Console.Write("Ошибка! Файл не найден!...попробуй ещё раз :P :)");
                return -1;
            }
            else
            {
                for (int i = 0; i < args.Length; i++)
                {
                    if (args[i] == "-in")
                    {
                        try { input = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-o" || args[i] == "--out")
                    {
                        try { output = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); } 
                    }
                    if (args[i] == "-i" || args[i] == "--include")
                    {
                        try { include = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-e" || args[i] == "--exclude")
                    {
                        try { exclude = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-l" || args[i] == "--lang")
                    {
                        try { lang = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-c" || args[i] == "--color")
                    {
                        try { color = args[i + 1]; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-r" || args[i] == "--recursive")
                    {
                        try { recursive = true; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "-m" || args[i] == "--mshelp")
                    {
                        try { mshelp = true; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }
                    if (args[i] == "--index")
                    {
                        try { index = true; }
                        catch { err = true; Console.WriteLine("Неверная команда"); }
                    }

                    if (input == "")
                        path = args[0];
                    else
                        path = input;
                }
                if (err)
                {
                    return -1;
                }
                else
                {
                    #region
                    try
                    {
                        ReadCode = File.OpenText(path);
                    }
                    catch
                    {
                        Console.WriteLine("Путь не существует");
                        return -1;
                    }
                    #endregion
                    /* "12343" */
                    /*
                    if (File.Exists(path))
                    {
                        ReadCode = File.OpenText(path);
                    }
                    else
                        if (Directory.Exists(path) && recursive == true)
                        {
                            foreach (string file in Directory.EnumerateFiles(path, "*." + lang, SearchOption.AllDirectories))
                            {

                            }
                        }
                        else
                        {
                            Console.WriteLine("Путь не существует");
                            return -1;
                        }
                     */

                    //if (output == "")
                    //{
                    //    WriteCode = File.CreateText(path + ".html");
                    //    if (color == "")
                    //        File.Copy(@"data\style.css", Path.GetDirectoryName(path) + "style.css", true);
                    //}
                    string slang;
                    FormatingModule formatCode = new FormatingModule();
                    
                    if (lang != "")
                    {
                        string str = ".";
                        str += lang;
                        slang = SelectedLang(str);
                    }
                    else
                        slang = SelectedLang(path);

                    if (color == "dark")
                    {
                        if (output == "")
                            File.Copy(baseDir + @"\data\" + slang + "dark.css", Path.GetDirectoryName(path) + @"\" + slang + "dark.css", true);
                        else
                            File.Copy(baseDir + @"\data\" + slang + "dark.css", Path.GetDirectoryName(output) + @"\" + slang + "dark.css", true);
                        formatCode.StyleColor = slang + "dark.css";
                    }
                    else
                        if (color != "")
                        {
                            if (output == "")
                                File.Copy(color, Path.GetDirectoryName(path) + @"\" + Path.GetFileName(color), true);
                            else
                                File.Copy(color, Path.GetDirectoryName(output) + @"\" + Path.GetFileName(color), true);
                            formatCode.StyleColor = Path.GetFileName(color);
                        }

                    string mystr;
                    if (output == "")
                    {
                        mystr = formatCode.Formating(ReadCode, slang, path, baseDir);
                    }
                    else
                    {
                        mystr = formatCode.Formating(ReadCode, slang, output, baseDir);
                    }

                    if (output == "")
                    {
                        File.WriteAllText(path + ".html", mystr);
                        if (color == "")
                            File.Copy(baseDir + @"\data\" + slang + ".css", Path.GetDirectoryName(path) + @"\" + slang + ".css", true);
                    }
                    else
                    {
                        File.WriteAllText(Path.GetDirectoryName(output) + "\"" + Path.GetFileName(path) + ".html", mystr);
                        if (color == "")
                            File.Copy(baseDir + @"\data\" + slang + ".css", Path.GetDirectoryName(output) + @"\" + slang + ".css", true);
                    }
                }
            }
            return 0;
        }

        static string SelectedLang(string name)
        {
            string str = "";
            for (int i = 0; i < name.Length; i++)
            {
                if (name[i] == '.')
                {
                    for (int j = i + 1; j < name.Length; j++)
                    {
                        str += name[j];
                    }
                    break;
                }

            }
            str = str.ToUpper();
            return str;
        }

    }

}